#include <stdio.h> 
#include <sys/socket.h> 
#include <string.h> 
#include <arpa/inet.h>
#include <unistd.h> 

int main(int argc, char **argv) {
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
        perror("socket error");
        return -1;
    }

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));

    server_addr.sin_port = htons(7777);
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_family = AF_INET;

    if (bind(fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0) {
        perror("bind error");
        return -1;
    }

    if (listen(fd, 2) < 0) {
        perror("listen error");
        return -1;
    }

    printf("start listen\n");

    while(1) {
        char buf[1024] = "";
        int clientfd = accept(fd, NULL, NULL);
        if (clientfd < 0) {
            perror("accept error");
            continue;
        }
        int ret = read(clientfd, buf, 1024);
        printf("recv:%s\n", buf);
        sleep(1);
    }
    return 0;
}
